package team.gl.nio.isncln;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import com.richman.controller.BetClient;

import com.richman.ps38.PS38Thread;
import com.richman.ps38.entity.BetDvalue;
import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.SendData;
import com.richman.utils.Common;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import team.gl.nio.cmn.Bag;
import team.gl.nio.cmn.ISNBag;
import team.gl.nio.cmn.NewBag;

public class ISNClientHandler extends ChannelInboundHandlerAdapter {  
	
	long lastreconnecttime = 0;
	
	
	
	public static ChannelHandlerContext ctx_s = null;	

    @Override  
    public void channelRead(ChannelHandlerContext ctx, Object msg)  
            throws Exception {
    	

    		ISNBag us = (ISNBag)msg;
        	
    		//备注皇冠  ISN
           	if(us.getReq().equals("isn")){
            		BetClient.mainWnd.updateMainWndStatusText(1, "ISNCLN与服务器连接正常");
            			if(BetClient.mainWnd != null &&   us != null) {
            				
            				try {
            					
            					BetClient.isnteamsLock.writeLock().lock();
            					BetClient.sendDatasLock.writeLock().lock();
            					
                				if(us.getTeams()!=null && us.getTeams().size() >0) {
                					BetClient.isnteams.clear();
                					for(int i= 0 ; i < us.getTeams().size(); i++) {
                						BetClient.isnteams.add(us.getTeams().elementAt(i));
                						
                					}
                				}
                				
                				BetClient.isnteamsLock.writeLock().unlock();
                				
                				
                				if(us.getSenddatas()!=null && us.getSenddatas().size() > 0) {
                					BetClient.sendDatas.clear();
                					for(int i = 0; i < us.getSenddatas().size(); i++) {
                						BetClient.sendDatas.add(us.getSenddatas().elementAt(i));
                					}
                				}
                				
                				BetClient.sendDatasLock.writeLock().unlock();
            					
            				}catch(Exception e) {
            					BetClient.isnteamsLock.writeLock().unlock();
            					BetClient.sendDatasLock.writeLock().unlock();
            					e.printStackTrace();
            				}
            				

//        					if(BetClient.betCtrMng != null) {
//        						BetClient.betCtrMng.bettest();
//        					}
            				
            			}

            		}

    	

    		
    		
    	}
   
	@Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("--- Server is active ---");
        ctx_s = ctx;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("--- Server is inactive ---");
        ISNNettyClient.channel = null;
        // 10s 之后尝试重新连接服务器
        System.out.println("2s 之后尝试重新连接服务器...");
        
        
        
        ISNNettyClient.group.schedule(new Runnable() {  
            
            @Override  
          
            public void run() {  
                while(ISNNettyClient.connect() == false) {
                	
                	try{  					
    					Thread.sleep(5000);
    					
    				}catch(Exception e){
    					e.printStackTrace();
    				} 
                }
                System.out.println("重新连接成功");
            }  
          
          }, 200L, TimeUnit.MILLISECONDS);  
          
        super.channelInactive(ctx);
    }

}